package com.billdowney.controller.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.billdowney.annotation.AuthRoleAllowed;
import com.billdowney.annotation.SystemLogInfo;
import com.billdowney.controller.BaseController;
import com.billdowney.util.auth.AuthRoleConst;
import com.billdowney.util.captcha.SecurityCode;
import com.billdowney.util.captcha.SecurityImage;
import com.billdowney.util.return_type.ReturnData;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.billdowney.util.captcha.SecurityCode.SecurityCodeLevel;
import com.billdowney.util.log.SystemLogHandleType;

/**
 * 工具栏控制器
 * 
 * @author 超级小富翁 - BillDowney
 * @data 2017年8月20日 下午10:20:51
 */
@RestController
@RequestMapping(value = "/util", method = { RequestMethod.POST, RequestMethod.GET })
@Api(tags = "工具控制器")
public class UtilController extends BaseController {

	@AuthRoleAllowed(AuthRoleConst.NOT_LOGIN)
	@RequestMapping(value = "/getServerDate")
	@SystemLogInfo(value = "获取服务器时间：{visit:realIp}", handleType = SystemLogHandleType.find)
	@ApiOperation(value = "获取服务器时间", notes = "格式为：{date:'yyyy-MM-dd HH:mm:ss',longTime:long型的时间戳}")
	public Object getServerDate() {
		ReturnData out = new ReturnData();
		Date date = new Date();
		Map<String, Object> data = new HashMap<>();
		// 放入时间对象，将自动格式化
		data.put("date", date);
		// 放入时间戳，供前台使用
		data.put("longDate", date.getTime());
		out.setData(data);
		return out;
	}

	@AuthRoleAllowed(AuthRoleConst.NOT_LOGIN)
	@RequestMapping(value = "/getSecurityCode")
	@SystemLogInfo(value = "获取验证码：{visit:realIp}", handleType = SystemLogHandleType.find)
	@ApiOperation(value = "获取验证码", notes = "返回一个前台可直接访问的base64加密的图片，格式：data:image/jpeg;base64,加密的图片内容", response = String.class)
	public Object getSecurityCode(@RequestParam(defaultValue = "6") int num) {
		String securityCode = SecurityCode.getSecurityCode(num, SecurityCodeLevel.Hard, false);
		return new ReturnData(SecurityImage.getImgStrBase64(securityCode, 200, 40));
	}

	@AuthRoleAllowed(AuthRoleConst.ROLE_SUPERADMIN)
	@RequestMapping(value = "/printSystemProperties")
	@ApiOperation(value = "打印系统变量", notes = "供后台观察使用", response = Map.class)
	public Object printSystemProperties() {
		ReturnData out = new ReturnData();
		Properties p = System.getProperties();
		Set<Object> keys = p.keySet();
		Map<Object, String> data = new HashMap<>();
		for (Object object : keys) {
			System.out.println(object + ":" + p.getProperty(object.toString()));
			data.put(object, p.getProperty(object.toString()));
		}
		out.setData(data);
		return out;
	}

}
